package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.DailyLogs;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 日报 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-07-27
 */
@Mapper
public interface DailyLogsMapper extends BaseMapper<DailyLogs> {

    /**
     * 分页列表
     * @param logsPage
     * @param wrapper
     * @return
     */
    @Select("<script>" +
            "select M.* from ( " +
            " SELECT a.*,b.username AS username FROM dragon_daily_logs a " +
            " LEFT JOIN dragon_admin b ON a.admin_id = b.id) M " +
            "${ew.customSqlSegment} "+
            "</script>")
    @DataScope(adminAlias = "M", adminField = "admin_id,reporter_id", deptAlias = "M", deptField = "department_id")
    Page<DailyLogs> dailyLogsList(Page<DailyLogs> logsPage, @Param(Constants.WRAPPER) QueryWrapper wrapper);

    /**
     * 提交列表
     * @param logsPage
     * @param wrapper
     * @return
     */
    @Select("select M.* from ( " +
            " SELECT a.*,b.username AS username FROM dragon_daily_logs a " +
            " LEFT JOIN dragon_admin b ON a.admin_id = b.id) M " +
            "${ew.customSqlSegment} ")
    Page<DailyLogs> dailyLogsSubmitList(Page<DailyLogs> logsPage, @Param(Constants.WRAPPER) QueryWrapper wrapper);

    /**
     * 接收列表
     * @param logsPage
     * @param wrapper
     * @return
     */
    @Select("select M.* from ( " +
            " SELECT a.*,b.username AS username FROM dragon_daily_logs a " +
            " LEFT JOIN dragon_admin b ON a.admin_id = b.id) M " +
            "${ew.customSqlSegment} ")
    Page<DailyLogs> dailyLogsReceptionList(Page<DailyLogs> logsPage, @Param(Constants.WRAPPER) QueryWrapper wrapper);

    @Select("SELECT count(*) FROM dragon_daily_logs ${ew.customSqlSegment}")
    @DataScope(adminField = "admin_id,reporter_id",isCheckDept = false)
    Long selectDailyLogsNum(@Param(Constants.WRAPPER) QueryWrapper<DailyLogs> wrapper);


    @Select("<script>" +
            "SELECT count(*) FROM dragon_daily_logs a WHERE left(a.create_time, 4) = left(#{time}, 4) " +
            "<if test='type != null and type != \"\"'>" +
            "AND a.type = #{type} "+
            "</if>"+
            "</script>")
    @DataScope(adminField = "admin_id,reporter_id",adminAlias = "a",isCheckDept = false)
    Long selectYearDailyLogs(String time, Integer type);

    @Select("select a.*,da.username as username from dragon_daily_logs a " +
            "left join dragon_admin da on da.id = a.admin_id ${ew.customSqlSegment}")
    List<DailyLogs> selectListByWrapper(@Param(Constants.WRAPPER) QueryWrapper<DailyLogs> wrapper);

    @Select("select a.*,da.username as username, da.img as userImg, da2.username as reporterName from dragon_daily_logs a " +
            "left join dragon_admin da on a.admin_id = da.id " +
            "left join dragon_admin da2 on a.reporter_id = da2.id where a.id = #{id} ")
    DailyLogs selectDetail(Integer id);
}
